import * as THREE from 'three'
import Manager from './Manager'
export default class Cube {
    materialColor = '#ffeded'
    objectsDistance = 4;
    sectionMeshes = []
    gradientTexture = null
    material = null

    constructor() {
        this.manager = new Manager();
        this.scene = this.manager.scene;
        this.create();
    }

    create() {
        this.createTexture();
        this.createMaterial();
        this.createMeshs();
    }

    /**
     * 创建贴图
     */
    createTexture() {
        const textureLoader = new THREE.TextureLoader();
        this.gradientTexture = textureLoader.load("./portfolio/gradients/3.jpg");
        this.gradientTexture.magFilter = THREE.NearestFilter;
    }
    /**
     * 创建材质
     */
    createMaterial() {
        this.material = new THREE.MeshToonMaterial({
            color: this.materialColor,
            gradientMap: this.gradientTexture,
        });
    }
    /**
     * 创建模型
     */
    createMeshs() {
        const mesh1 = new THREE.Mesh(
            new THREE.TorusGeometry(1, 0.4, 16, 60),
            this.material
        );
        const mesh2 = new THREE.Mesh(new THREE.OctahedronGeometry(1.4), this.material);
        const mesh3 = new THREE.Mesh(
            new THREE.TorusKnotGeometry(0.8, 0.35, 100, 16),
            this.material
        );

        mesh1.position.x = 1.5;
        mesh2.position.x = -1.5;
        mesh3.position.x = 1.5;

        mesh1.position.y = -this.objectsDistance * 0;
        mesh2.position.y = -this.objectsDistance * 1;
        mesh3.position.y = -this.objectsDistance * 2;

        this.scene.add(mesh1, mesh2, mesh3);

        this.sectionMeshes = [mesh1, mesh2, mesh3];
    }

    update() {
        for (let i of this.sectionMeshes) {
            i.rotation.x += this.manager.time.deltaTime * 0.1;
            i.rotation.y += this.manager.time.deltaTime * 0.12;
        }
    }
}